<template>
	<view class="w-picker-view">
		<picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
			<picker-view-column>
				<view class="w-picker-item" v-for="(item,index) in range.years" :key="index">{{item}}年</view>
			</picker-view-column>
			<picker-view-column>
				<view class="w-picker-item" v-for="(item,index) in range.quarters" :key="index">第{{item}}季度</view>
			</picker-view-column>
		</picker-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pickVal:[],
				range:{},
				checkObj:{},
				list:[],
				show:true
			};
		},
		props:{
			itemHeight:{
				type:String,
				default:"44px"
			},
			startYear:{
				type:String,
				default:""
			},
			endYear:{
				type:String,
				default:""
			},
			value:{
				type:[String,Array,Number],
				default:""
			},
			current:{//是否默认选中当前日期
				type:Boolean,
				default:false
			},
		},
		watch:{
			// value(val){
			// 	console.log('watch',val)
			// 	this.initData();
			// }
		},
		created() {
			this.initData();
		},
		methods:{
			formatNum(n){
				return (Number(n)<10?'0'+Number(n):Number(n)+'');
			},
			checkValue(value){
				let strReg=/^\d{4}-\d{2}-\d{2}$/,example;
				if(!strReg.test(value)){
					console.log(new Error("请传入与mode、fields匹配的value值，例value="+example+""))
				}
				return strReg.test(value);
			},
			getData(dVal){
				//用来处理初始化数据
				console.log('处理数据',dVal)
				let years=[],quarters=[];
				let startYear=this.getStartDate().getFullYear();
				let endYear=this.getEndDate().getFullYear();
				let year=dVal[0]*1;
				for(let year=startYear;year<=endYear;year++){
					years.push(year.toString())
				}
				for(let quarter=1;quarter<=4;quarter++){
					quarters.push(quarter);
				}
				return {
					years,
					quarters
				}
			},
			getCurrenDate(){
				let curDate=new Date();
				let curYear=curDate.getFullYear();
				let curMonth=curDate.getMonth()+1;
				let curMonthdays=new Date(curYear,curMonth,0).getDate();
				let curDay=curDate.getDate();
				
				return{
					curDate,
					curYear,
					curMonth,
					curMonthdays,
				}
			},
			getDefaultDate(){
				let value=this.value;
				let reg=/-/g;
				let defaultDate=value?new Date(value.split(" ")[0].replace(reg,"/")):new Date();
				let defaultYear=defaultDate.getFullYear();
				let defaultMonth=defaultDate.getMonth()+1;
				let defaultDay=defaultDate.getDate();
				return{
					defaultDate,
					defaultYear,
					defaultMonth,
					defaultDay,
				}
			},
			getStartDate(){
				let start=this.startYear;
				let startDate="";
				let reg=/-/g;
				if(start){
					startDate=new Date(start+"/01/01");
				}else{
					startDate=new Date("1970/01/01");
				}
				return startDate;
			},
			getEndDate(){
				let end=this.endYear;
				let reg=/-/g;
				let endDate="";
				if(end){
					endDate=new Date(end+"/12/31");
				}else{
					endDate=new Date();
				}
				return endDate;
			},
			getDval(){
				let value=this.value;
				let dVal=null;
				let aDate=new Date();
				let year=this.formatNum(aDate.getFullYear());
				let month=this.formatNum(aDate.getMonth()+1);
				let quarter = month*1 < 4 ? 1 : month*1 < 7 ? 2 : month*1 < 10 ? 3 : 4; 
				if(value){
					let flag=this.checkValue(value);
					if(!flag){
						dVal=[year,quarter]
					}else{
						let v=value.split(" ");
						let valList = v[0].split('-');
						quarter = valList[1]*1 < 4 ? 1 : valList[1]*1 < 7 ? 2 : valList[1]*1 < 10 ? 3 : 4; 
						dVal=[valList[0],quarter];
					}
				}else{
					dVal=[year,quarter]
				}
				console.log(dVal)
				return dVal;
			},
			initData(){
				console.log('init')
				let years=[],quarters=[];
				let dVal=this.getDval();
				let dateData=this.getData(dVal);
				years = dateData.years;
				quarters = dateData.quarters;
				this.range={years,quarters};
				let pickVal=[
					dVal[0]&&years.indexOf(dVal[0])!=-1?years.indexOf(dVal[0]):0,
					dVal[1]&&quarters.indexOf(dVal[1])!=-1?quarters.indexOf(dVal[1]):0,
				];
				this.$nextTick(()=>{
					this.pickVal=pickVal;
				});
				let year=dVal[0]?dVal[0]:years[0];
				let quarter=dVal[1]?dVal[1]:quarters[0];

				let result="",full="",obj={};
				result=full=`${year+ ' ' +quarter}`;
				obj={
					year,
					quarter,
				}
				
				this.checkObj = obj;
				
				this.$emit("change",{
					result:result,
					value:full,
					obj:obj
				})
				
			},
			handlerChange(e){
				let arr=[...e.detail.value];
				let data = this.range;
				let year="",quarter="";
				year = (arr[0]||arr[0]==0) ? data.years[arr[0]] || data.years[data.years.length-1] : "";
				quarter = (arr[1]||arr[1]==0) ? data.quarters[arr[1]] || data.quarters[data.quarters.length-1] : "";
				let pickVal = this.pickVal;

				let result="",full="",obj={};
				result=full=`${year+' '+quarter}`;
				obj={
					year,
					quarter,
				}
				
				this.checkObj = obj;
				this.pickVal = arr;
				
				this.$emit("change",{
					result:result,
					value:full,
					obj:obj
				})
				
			},

		}
	}
</script>

<style lang="scss">
	@import "./w-picker.css";
</style>
